package org.artifact.builder.design;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.artifact.builder.enums.BuilderMethodEnum;
import org.artifact.core.cache.AbstractCache;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Getter
@Setter
@ToString
public class DesignTable {
	/** 表名称 */
	private String tableName;
	/** 类名称 */
	private String className;
	/** 备注 */
	private String remark;
	/** 字段集 */
	private List<DesignColumn> columns = new ArrayList<>();
	/** 主键 */
	private DesignColumn pk;
	/** 方法 */
	private Set<String> methods = new HashSet<>();
	/** 索引 */
	private List<DesignIndex> indexs = new ArrayList<>();
	/** 关系 */
	private List<DesignRelation> relations = new ArrayList<>();
	/** 是否是同步操作 */
	private Boolean sync;
	/** 缓存 */
	private Class<? extends AbstractCache> cache;

	public DesignTable(Element table,DesignConfig designConfig) {
		this.tableName = table.getAttribute("name");
		
		// 1.去除前缀
		className = StrUtil.removePrefix(this.tableName,designConfig.getTablePrefix());
		// 2.转驼峰
		className = StrUtil.toCamelCase(className);
		// 3.首字母大写
		className = StrUtil.upperFirst(className);
		
		this.remark = table.getAttribute("remark");
		
		BuilderMethodEnum[] methodEnum = designConfig.getMethods();
		for (BuilderMethodEnum builderMethodEnum : methodEnum) {
			methods.add(builderMethodEnum.name().toLowerCase());
		}
		String[] methodArray = StrUtil.split(table.getAttribute("methods"), ",");
		for (String method : methodArray) {
			methods.add(method);
		}
		
		// 字段
		List<Element> columns = XmlUtil.getElements(XmlUtil.getElement(table, "columns"), "column");
		for (Element element : columns) {
			DesignColumn column = new DesignColumn(element,designConfig);
			if (this.pk==null && column.isPk()) {
				this.pk = column;
			}
			this.columns.add(column);
		}
		if (!designConfig.isLogMode()){
			this.columns.add(new DesignColumn("create_date","createDate", "java.util.Date",false, "创建时间", false,"","datetime",""));
			this.columns.add(new DesignColumn("modified_date","modifiedDate", "java.util.Date",false, "修改时间", false,"","datetime",""));
		}
		if (designConfig.isLogMode()){
			this.columns.add(new DesignColumn("log_date","logDate", "java.util.Date",false, "日志时间", false,"","datetime",""));
		}

		
		
		// 常规索引
		if (XmlUtil.getElement(table, "indexs")!=null) {
			List<Element> indexs = XmlUtil.getElements(XmlUtil.getElement(table, "indexs"), "index");
			for (Element element : indexs) {
				DesignIndex index = new DesignIndex(this,element);
				this.indexs.add(index);
			}
		}

		String sync = table.getAttribute("sync");
		if (StrUtil.isNotBlank(sync)){
			this.sync = Boolean.valueOf(sync);
		}

		String cacheClass = table.getAttribute("cache");
		if (StrUtil.isNotBlank(cacheClass)){
			this.cache = ClassUtil.loadClass(cacheClass);
		}
	}
	
	public DesignColumn getByName(String name) {
		for (DesignColumn column : columns) {
			if (column.getColumnName().equals(name)) {
				return column;
			}
		}
		return null;
	}
}
